package com.patterns.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * 主题对象
 * 发布者通过此类给订阅者推送消息
 * 订阅者通过此类订阅发布者的消息
 * @author huangke
 *
 */
public abstract class Subject {
	protected List<Observer> list = new ArrayList<Observer>();
	
	public void register(Observer o){//订阅者订阅消息
		this.list.add(o);
	}
	
	public void remove(Observer o){//订阅者取消订阅
		this.list.remove(o);
	}
	//发布者推送消息给订阅者
	public void notify(String msg){
		for (Observer observer : list) {
			observer.update(msg);
		}
	}
	
	public abstract void send(String msg);
}
